﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using RESTServicesCliente.Dominio;
using RESTServicesCliente.Persistencia;
using System.ServiceModel.Web;
using System.Net;

namespace RESTServicesCliente
{
    public class Clientes : IClientes
    {
        private ClientesDAO dao = new ClientesDAO();

        public Cliente CrearClientes(Cliente clienteACrear)
        {
            try
            {
                Cliente existe = ObtenerClientes(clienteACrear.Dni.ToString());
                if (existe != null) {
                    throw new WebFaultException(HttpStatusCode.NotAcceptable);
                }else{
                    return dao.Crear(clienteACrear);
                } 
            }
            catch (WebFaultException ex)
            {
                throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
            }
        }

        public Cliente ObtenerClientes(string dni)
        {
            return dao.Obtener(Convert.ToInt32(dni));
        }

        public Cliente ModificarClientes(Cliente clienteAModificar)
        {
            return dao.Modificar(clienteAModificar);
        }

        public void EliminarClientes(Cliente clienteAEliminar)
        {
            try
            {
                Cliente existe = ObtenerClientes(clienteAEliminar.Dni.ToString());
                if (existe == null)
                {
                    throw new WebFaultException(HttpStatusCode.NotAcceptable);
                }
                else
                {
                    dao.Eliminar(clienteAEliminar);
                }
            }
            catch (WebFaultException ex)
            {
                throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
            }
        }

        public List<Cliente> ListarClientes()
        {
            return dao.ListarTodos();
        }

    }
}
